﻿using UnityEngine;
using System.Collections;

namespace play {
	public class Fire : MonoBehaviour {
		Ctrl ctrl;
		private Ctrl Player;
		private float bulletOffset = 0.75f;
		public EasyButton shotBtn;
		public Vector3 pos;

		void Start(){
			if (Application.platform != RuntimePlatform.Android) {
				return;
			}

			ctrl = GetComponent<Ctrl> ();
			pos = transform.position;
			Player = transform.GetComponent<Ctrl>();
			shotBtn = GameObject.FindObjectOfType<EasyButton>();
			//绑定坦克
			shotBtn.receiverGameObject = transform.root.gameObject;
		}
		
		void Test(){
			Debug.Log("Gun Test");

		}
		void shot(){	
			Debug.Log("shot!");
			pos = transform.position;
			Vector3 bulletPos = Vector3.zero;
			int direction = Common.UP ;

			// 死了就不能打了,打过了也不能打
			if (!ctrl.state.isAlive||!ctrl.state.readyToFire) {
				return;
			}

			if (Player.state.direction == Common.UP) {
				direction = Common.UP;
				bulletPos = new Vector3(pos.x, pos.y + bulletOffset, pos.z);
			}
			else if (Player.state.direction == Common.DOWN) {
				direction = Common.DOWN;
				bulletPos = new Vector3(pos.x, pos.y - bulletOffset, pos.z);
			}
			else if( Player.state.direction == Common.LEFT) {
				direction = Common.LEFT;
				bulletPos = new Vector3(pos.x - bulletOffset, pos.y, pos.z);
			}
			else if (Player.state.direction == Common.RIGHT) {
				direction = Common.RIGHT;
				bulletPos = new Vector3(pos.x + bulletOffset, pos.y, pos.z);
			}
			
			new Bullet(direction, bulletPos, gameObject,true);

			ctrl.state.readyToFire = false;
			// biu
			AudioSource audioSource = GetComponent<AudioSource> ();
			audioSource.clip = Resources.Load ("mp3/biu") as AudioClip;
			audioSource.Play ();
		}
	}
}